﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FileToBase64
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\IA\Desktop\fonteditor.woff";
            string base64 = FileToBase64Str(path);
            Console.WriteLine(base64);
            Console.ReadKey();
        }

        /// <summary>
        /// 文件转为base64编码
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string FileToBase64Str(string filePath)
        {
            try
            {
                using (FileStream fs = File.OpenRead(filePath))
                {
                    byte[] bt = new byte[fs.Length];
                    fs.Read(bt, 0, bt.Length);
                    return Convert.ToBase64String(bt);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("读取文件发生错误：" + ex.Message);
                return "";
            }
        }
    }
}
